//
//  ViewController.m
//  BSoftWorkLog
//
//  Created by zarra on 2017/8/3.
//  Copyright © 2017年 zarra. All rights reserved.
//

#import "ViewController.h"
#import "LoginViewController.h"

@interface ViewController ()

-(void)showLogin;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    __weak BSoftLogService* service = [BSoftLogService shareInstance];
    __weak ViewController* this = self;
    
    self.roomValue = kHCDictRoomNot;
    [this.editToday setText:@""];
    [this.editWorkLog setText:@""];
    
    if ([service isUserNameAndPasswordOK]) {
        NSString* username = [service getVarForKey:KeyUserName];
        NSString* password = [service getVarForKey:KeyPassword];
        
        [MBProgressHUD showMessage:@"正在加载。。。" toView:self.view ];
        
        [service userLogin:username password:password completionHandler:^(NSError *error) {
            if (error!=nil) {
                [this showLogin];
            }else{
                
                [service getTodayLog:username completionHandler:^(NSString *dict, NSError *error) {
                    
                    [this.editToday setText:dict];
                    
                    [service queryLastProject:username completionHandler:^(NSDictionary *dict, NSError *error) {
                        [MBProgressHUD hideHUDForView:self.view];
                        //同时弹出“加载成功”的提示；
                        [MBProgressHUD showSuccess:@"加载成功"];
                
                        [this.bProject setTitle:[[[BSoftLogService shareInstance]project] projectName] forState:UIControlStateNormal];
                    }];
                }];
                
                
               
            }
        }];
        
    }else{
        [self showLogin];
    }
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)showLogin{
    UINavigationController* nav = self.navigationController;
    
    UIViewController* loginViewController =
    [[UIStoryboard storyboardWithName:@"Main"
                               bundle:NULL] instantiateViewControllerWithIdentifier:@"LoginVC"];
    
    [nav setViewControllers:@[loginViewController] animated:YES];
}

- (IBAction)rootSegmentValueChanged:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        self.roomValue = kHCDictRoomNot;
    }
    else if (sender.selectedSegmentIndex == 1) {
        self.roomValue = kHCDictRoomDorm;
    }
    else{
        self.roomValue = kHCDictRoomHotel;
    }
}


- (IBAction)clickLogout:(id)sender {
    __weak BSoftLogService* service = [BSoftLogService shareInstance];
    
    [service putVar:nil forKey:KeyUserName];
    [service putVar:nil forKey:KeyPassword];
    
    [self showLogin];
}

- (void)refreshEditToday:(BSoftLogService *)service WithName:(NSString*)username{
    [service getTodayLog:username completionHandler:^(NSString *dict, NSError *error) {
        
        [self.editToday setText:dict];
        
        [service queryLastProject:username completionHandler:^(NSDictionary *dict, NSError *error) {
//            [MBProgressHUD hideHUDForView:self.view];
//            //同时弹出“加载成功”的提示；
            [MBProgressHUD showSuccess:@"刷新成功"];
            
            [self.bProject setTitle:[[[BSoftLogService shareInstance]project] projectName] forState:UIControlStateNormal];
        }];
    }];
}

- (IBAction)clickSubmit:(id)sender {
    
    __weak BSoftLogService* service = [BSoftLogService shareInstance];
     __weak ViewController* this = self;
    NSString* username = [service getVarForKey:KeyUserName];
    
    NSString* content = self.editWorkLog.text;
    
    BSoftWorkLog* workLog = [[BSoftWorkLog alloc]init];
    
    workLog.username = username;
    workLog.content = content;
    workLog.score = self.tfScore.text;
    workLog.rzqk = self.roomValue;
    
    if (self.sOut.isOn) {
        workLog.ccbz = @"1";
    }
    
    [[BSoftLogService shareInstance]saveWorkLog:workLog completionHandler:^(NSDictionary *dict, NSError *error) {
          dispatch_async(dispatch_get_main_queue(), ^{
//        [self loadView];
              [this refreshEditToday:service WithName:username];
          });
    }];

}


@end
